package com.yuanheng100.ems4j.dingtalk.media;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.open.client.ServiceFactory;
import com.dingtalk.open.client.api.model.corp.UploadResult;
import com.dingtalk.open.client.api.service.corp.MediaService;
import com.yuanheng100.ems4j.dingtalk.config.DingTalkEnv;
import com.yuanheng100.ems4j.dingtalk.utils.HttpHelper;

import java.io.File;

public class MediaHelper
{
    public enum Type
    {
        IMAGE, VOICE, VIDEO, FILE;
    }

    public static UploadResult upload(String accessToken, Type type, File file) throws Exception
    {

        MediaService mediaService = ServiceFactory.getInstance().getOpenService(MediaService.class);
        UploadResult uploadResult = mediaService.uploadMediaFile(accessToken, type.name().toLowerCase(), file);
        return uploadResult;
    }


    public static void download(String accessToken, String mediaId, String fileDir) throws Exception
    {

        String url = DingTalkEnv.OAPI_HOST + "/media/get?" +
                "access_token=" + accessToken + "&media_id=" + mediaId;
        JSONObject response = HttpHelper.downloadMedia(url, fileDir);
    }
}
